"""

 Connection module
 $Id: meta.py 450 2012-04-13 12:35:55Z andre $

"""

# from sqlalchemy.ext.declarative import declarative_base
# from sqlalchemy.orm import scoped_session, sessionmaker
# from sqlalchemy import create_engine, MetaData

from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

import logging
log = logging.getLogger(__name__)

# engine=create_engine('postgresql://postgres:postgres@athena.cube.lan/postdove', echo=True)
# engine=create_engine('postgresql://postgres:postgres@zeus.cube.lan/postdove', echo=False)

engineDev='postgresql://postgres:postgres@athena.cube.lan/postdove'
engineProd='postgresql://postgres:postgres@zeus.cube.lan/postdove'

engine=None

Session = scoped_session(sessionmaker())
session = Session

metadata=MetaData()

def init_cubemail(pySession, pyMetadata):
    """
    Append a call to this function at the end of init_model
    (PROJECT/model/meta.py) to set Session in cubemail equal
    to Session in PROJECT
    """
    global Session, metadata, engine

    log.debug('init_cubemail: session %r - metadata %r' % (pySession,
        pyMetadata))

    metadata=pyMetadata
    Session=pySession
    engine=Session.bind.engine

def init_engine(engineurl, echo=True):
    """
    Call this function in non-pylons applications to setup connection to DB
    """
    global session, engine

    engine=create_engine(engineurl, echo=echo)
    Session.configure(bind=engine)
